﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin1_SupportOnline : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        rpt_Category.DataSource = db.SupportOnlines;
        rpt_Category.DataBind();
    }
    protected void lbt_Add_New_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = true;

        panel_Edit.Visible = false;
    }

    protected void btnxoa_Click(object sender, EventArgs e)
    {
        int supid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        //Response.Write("<script>alert("+id+")</script>"); kiem tra id 
        //1. tim
        var sup = db.SupportOnlines.Single(c => c.Id == supid);

        //2. Xoa
        db.SupportOnlines.DeleteOnSubmit(sup);
        //3. luu
        db.SubmitChanges();
        Response.Redirect("SupportOnline.aspx");
    }
    protected void lbt_Edit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = true;
        panel_Add.Visible = false;
        int supid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        var sup = db.SupportOnlines.Single(c => c.Id == supid);
        txt_Name_Edit.Text = sup.Name;
        txt_Supid.Text = supid.ToString();
        txt_Skype_Edit.Text = sup.Skype;
        txt_Yahoo_Edit.Text = sup.Yahoo;
        txt_Phone_Edit.Text = sup.Phone;
        if (sup.Activated == true)
        {
            rad_Edit.SelectedIndex = 0;
        }
        else
        {
            rad_Edit.SelectedIndex = 1;
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            var sup = new SupportOnline
            {
                Name=txt_Name.Text,
                Skype = txt_Skpye.Text,
                Yahoo = txt_Yahoo.Text,
                Phone = txt_Phone.Text,
                Activated = bool.Parse(radAdd.SelectedValue),
            };
            db.SupportOnlines.InsertOnSubmit(sup);
            db.SubmitChanges();
            Response.Redirect("SupportOnline.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<script> alert('Thêm mới không thành công! ');</script>");
        }
    }
    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
        txt_Phone.Text = txt_Skpye.Text = txt_Yahoo.Text = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            // tim 
            var sup = db.SupportOnlines.Single(c => c.Id == int.Parse(txt_Supid.Text));
            sup.Name = txt_Name_Edit.Text;
            sup.Skype = txt_Skype_Edit.Text;
            sup.Yahoo = txt_Yahoo_Edit.Text;
            sup.Phone = txt_Phone_Edit.Text;
            sup.Activated = bool.Parse(rad_Edit.SelectedValue);
            //3 Luu
            db.SubmitChanges();
            panel_Edit.Visible = false;
            Response.Redirect("SupportOnline.aspx");
        }
        catch (Exception ex)
        {

            Response.Write("<script> alert('Chỉnh sửa không thành công! ');</script>");

        }
    }
    protected void lbt_Close_Edit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
    }
}